#include <thread>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <list>

using std::cout;
using std::endl;

std::mutex mtx;
std::condition_variable cv;

int cargo = 0;
bool shipment_available(){ return cargo != 0; }

void consume (int n)
{
    for(int i = 0; i < n; ++i)
    {
        std::unique_lock<std::mutex> lck(mtx);
        cv.wait(lck, shipment_available); //A callable object or function that takes no arguments and returns a value that can be evaluated as a bool.  This is called repeatedly until it evaluates to true.

        //coonsume:
        cout << cargo << "\n";
        cargo = 0;
    }
}

int main()
{
    std::thread consumer_thread (consume, 10);

    //produce 10 items when needed:
    for(int i = 0; i < 10; ++i)
    {
        while(shipment_available()) std::this_thread::yield(); //该命名空间对访问当前线程的一组函数进行分组。

        std::unique_lock<std::mutex> lck(mtx);
        cargo = i +1;

        cv.notify_one();
    }

    consumer_thread.join();
}
